<?php
import( 'tools.Setup.interfaces.SetupScreen' );
import( 'tools.Setup.classes.DatabaseSetupScreen' );
class ReporterAccountScreen extends DatabaseSetupScreen implements SetupScreen
{
	public function __construct( $config, $words, $screenID, $path )
	{
		parent::__construct( $config, $words, $screenID, $path );
		$this->connectDatabase( parse_ini_file( $this->config['sources']['db_access'] ) );
	}

	public function handleAction( $action )
	{
		if( $action == "saveReporterAccount" )
		{
			if( !$_POST['username'] )
				throw new Exception( 'User Name is not defined.' );
			if( !$_POST['email'] )
				throw new Exception( 'Mail Address is not defined.' );
			if( !$_POST['password'] )
				throw new Exception( 'Password is not defined.' );

			$query	= "UPDATE {#prefix#}users SET username=\"".$_POST['username']."\", email=\"".$_POST['email']."\", password=\"".md5( $_POST['password'] )."\" WHERE user_id=4";
			if( !$this->queryDatabase( $query ) )
				throw new Exception( "Reporter Account could not been updated." );
			return true;
		}
	}

	public function getView()
	{
		$query	= "SELECT * FROM {#prefix#}users WHERE user_id=4";
		$result	= $this->queryDatabase( $query );
		$data	= mysql_fetch_array( $result );
		unset( $data['password'] );
	
		if( $this->config['fields'] )
		{
			foreach( $this->config['fields'] as $fieldName => $fieldDefinition )
			{
				$ui["label_".$fieldName]	= "<label for='".$fieldName."'>".$this->words['fields'][$fieldName]."</label>";
				if( $fieldDefinition['type'] == "input" )
				{
					$ui["field_".$fieldName]	= Elements::Input( $fieldName, $data[$fieldName] );
				}
			}
		}
		return $this->loadTemplate( $ui );
	}
}
?>